package cn.jl.demo;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author LongJunLin
 * @version 创建时间：2021年1月15日 下午1:18:49
 * @Description 类描述
 */
@Data
@Slf4j
public class TestSyn {

	private int i = 0;

	private synchronized void test1() {
		i = i + 1;
		log.info(i + "");
	}

	private synchronized void test2() {

		while (true) {

		}
	}

	public static void main(String[] args) {
		TestSyn testSyn = new TestSyn();
		// 获得锁，并且不释放
		Runnable r2 = () -> {
			testSyn.test2();
		};
		Thread t2 = new Thread(r2, "永远线程：");
		t2.start();
		Runnable r1 = () -> {
			System.out.println("进入线程+准备尝试获取锁");
			testSyn.test1();
		};

		Thread t1 = new Thread(r1, "test1");
		t1.start();
		t1.interrupt();
	}
}
